package com.wjk.kylin.mall.auth.security.core.userdetails.member;

import com.alibaba.fastjson.JSONObject;
import com.wjk.kylin.mall.auth.feign.MemberFeignService;
import com.wjk.kylin.mall.common.core.bo.member.MemberUserNameBO;
import com.wjk.kylin.mall.common.core.constant.Constants;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import com.wjk.kylin.mall.common.core.dto.member.MemberUserNameInDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 商城会员用户认证服务
 *
 * @author <a href="mailto:xianrui0365@163.com">xianrui</a>
 */
@Slf4j
@Service
public class MemberUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public UserDetails loadUserByUsername(String username) {

        MemberUserDetails userDetails = new MemberUserDetails(getMemberUserNameBO(username));

        if (userDetails == null) {
            throw new UsernameNotFoundException("登录用户：[" + username + "]不存在");
        } else if (!userDetails.isEnabled()) {
            throw new DisabledException("对不起，您的账号：[" + username + "]已禁用");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("对不起，您的账号：[" + username + "]已锁定");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("对不起，您的账号：[" + username + "]已过期");
        }

        log.info("登录用户：[{}]，用户信息：[{}]", username, JSONObject.toJSONString(userDetails));
        return userDetails;
    }
    /**
     * 查询商城会员用户
     *
     * @param username
     * @return
     */
    private MemberUserNameBO getMemberUserNameBO(String username) {
        MemberUserNameInDTO inDTO = new MemberUserNameInDTO();
        inDTO.setUsername(username);
        DataOutDTO<MemberUserNameBO> outDTO = memberFeignService.loadUserByUsername(inDTO);
        if (outDTO == null || outDTO.getCode() != Constants.SUCCESS || outDTO.getData() == null) {
            throw new UsernameNotFoundException("登录用户：[" + username + "]不存在");
        }
        return outDTO.getData();
    }


//    /**
//     * 手机号码认证方式
//     *
//     * @param mobile
//     * @return
//     */
//    public UserDetails loadUserByMobile(String mobile) {
//        MemberUserDetails userDetails = null;
//        Result<MemberAuthDTO> result = memberFeignClient.loadUserByMobile(mobile);
//        if (Result.isSuccess(result)) {
//            MemberAuthDTO member = result.getData();
//            if (null != member) {
//                userDetails = new MemberUserDetails(member);
//                userDetails.setAuthenticationMethod(AuthenticationMethodEnum.MOBILE.getValue());   // 认证方式：OpenId
//            }
//        }
//        if (userDetails == null) {
//            throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
//        } else if (!userDetails.isEnabled()) {
//            throw new DisabledException("该账户已被禁用!");
//        } else if (!userDetails.isAccountNonLocked()) {
//            throw new LockedException("该账号已被锁定!");
//        } else if (!userDetails.isAccountNonExpired()) {
//            throw new AccountExpiredException("该账号已过期!");
//        }
//        return userDetails;
//    }
//
//    /**
//     * openid 认证方式
//     *
//     * @param openId
//     * @return
//     */
//    public UserDetails loadUserByOpenId(String openId) {
//        MemberUserDetails userDetails = null;
//        Result<MemberAuthDTO> result = memberFeignClient.loadUserByOpenId(openId);
//        if (Result.isSuccess(result)) {
//            MemberAuthDTO member = result.getData();
//            if (null != member) {
//                userDetails = new MemberUserDetails(member);
//                userDetails.setAuthenticationMethod(AuthenticationMethodEnum.OPENID.getValue());   // 认证方式：OpenId
//            }
//        }
//        if (userDetails == null) {
//            throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
//        } else if (!userDetails.isEnabled()) {
//            throw new DisabledException("该账户已被禁用!");
//        } else if (!userDetails.isAccountNonLocked()) {
//            throw new LockedException("该账号已被锁定!");
//        } else if (!userDetails.isAccountNonExpired()) {
//            throw new AccountExpiredException("该账号已过期!");
//        }
//        return userDetails;
//    }
}
